/*
 * DatabaseInfo.java
 *
 * Created on September 5, 2007, 9:11 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package gblend.db;

import java.io.Serializable;
import java.sql.Connection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * This is a container for the database information. 
 * A dababase engine is also provided. Any database ralated task
 * should be performed through the database engine API.
 * @author Colin
 */
public class DatabaseInfo implements Serializable{

    private static final long serialVersionUID = 10000L;
    
    public static final String SCHEMA_TREE_ROOT = "<root>";
    
    public static int HOST_NAME = 0;
    public static int PORT_NUMBER = 1;
    public static int DB_NAME = 2;
    public static int USER_NAME = 3;
    public static int PASSWORD = 4;
    
    // database parameters
    // unmodifiable, user must provide all these parameters in the constructor
    // currently hostname and port are not used
    private String hostName = "localhost";
    private String port = "1433";
    private String dbName;
    private String userName;
    private String password;

    /*private String[] dblabels ={"C","O","Cu","N","S","P","Cl","Zn","B","Br","Co","Mn","As","Al","Ni","Se","Si",
    "V","Sn","I","F","Li","Sb","Fe","Pd","Hg","Bi","Na","Ca","Ti","Ho","Ge","Pt","Ru","Rh","Cr","Ga","K","Ag","Au",
    "Tb","Ir","Te","Mg","Pb","W","Cs","Mo","Re","Cd","Os","Pr","Nd","Sm","Gd","Yb","Er","U","Tl","Ac"};*/
    private String[] dblabels ={"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19",
"20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39",
"40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59",
"60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79",
"80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99",
"100","101","102","103","104","105","106","107","108","109","110","111","112","113","114","115","116","117","118","119",
"120","121","122","123","124","125","126","127","128","129","130","131","132","133","134","135","136","137","138","139",
"140","141","142","143","144","145","146","147","148","149","150","151","152","153","154","155","156","157","158","159",
"160","161","162","163","164","165","166","168","169","170","171","172","173","174","175","176","177","178","179","181",
"182","183","184","185","186","187","188","190","191","192","193","196","197","198","199","200","201","203","204","206",
"207","208","209","210","211","212","215","216","217","219","220","221","223","225","227","228","229","230","231","232",
"236","238","241","242","244","245","246","247","248","249","250","251","253","254","255","256","260","261","262","264",
"265","272","274","275","277","278","280","281","283","286","288","289","290","291","294","295","296","302","305","317",
"322","323","324","327","328","329","334","336","337","338","339","351","353","371","374","401","408","415","417","419",
"424","429","438","439","444","462","498","500","504","548","580","583","589","599","643","678","684","731","760","1069"
};

    // database engine that provides service to this database
    private transient DatabaseEngine engine;
    
    // the database connection
    private transient Connection dbConnection;
    
    // connection status
    private transient boolean connected = false;
    
    // status code if schema is available
    private transient boolean schemaAvailable = false;
    
    public DatabaseInfo() {
    }

    public DatabaseInfo(
            String dbName,
            String userName,
            String password) {
        this.dbName = dbName;
        this.userName = userName;
        this.password = password;
    }

    public DatabaseInfo(String[] metaInfo, DatabaseEngine engine){
        hostName = "localhost";
        port = "1433";
        dbName = metaInfo[DatabaseInfo.DB_NAME];
        userName = metaInfo[DatabaseInfo.USER_NAME];
        password = metaInfo[DatabaseInfo.PASSWORD];
        this.engine = engine;
    }
    
    public void connect() throws Exception{
        dbConnection = getEngine().connect();
        this.setConnected(true);
    }
    
    public String getDbName() {
        return dbName;
    }

    @Override
    public String toString() {
        return dbName + "@" + getHostName();
    }

    public String getHostName() {
        return hostName;
    }

    public String getPort() {
        return port;
    }

    public String getUserName() {
        return userName;
    }

    public String getPassword() {
        return password;
    }

    public DatabaseEngine getEngine() {
        return engine;
    }

      public String[] getLabels() {
        return dblabels;
    }

   
    public boolean isConnected() {
        return connected;
    }

    public void setConnected(boolean connected) {
        this.connected = connected;
    }
    
    public List<String> getDataSourceList() {
        List<String> list = new LinkedList<String>(getEngine().getDataSourceSet());
        return Collections.unmodifiableList(list);
    }

    public Connection getDbConnection() {
        return dbConnection;
    }

    public void setEngine(DatabaseEngine engine) {
        if(getEngine() == null)
            this.engine = engine;
    }

    public boolean isSchemaAvailable() {
        return schemaAvailable;
    }

    public void setSchemaAvailable(boolean schemaAvailable) {
        this.schemaAvailable = schemaAvailable;
    }
}
